<?php
class event_md extends CI_Model{

    function  __construct() {
        parent::__construct();
    }

function getAllEvents(){
         // need to get all the event that are not expire to be displayed to user
         $eQuery="SELECT ve.eid,ve.state,ve.city,ve.zipcode,ve.eName,ve.tStamp,ve.isExpire,ve.eDesc,ve.cost
             FROM vevents AS ve where isExpire !=1";
         $rs=$this->db->query($eQuery);
//var_dump($rs->result());
         return $rs->result();
    }


    function getUserEvents($uid){
        //need to get user register events
        $uQuery="SELECT * FROM vevents AS ve LEFT JOIN eregistar AS er ON ve.eid=er.eid where er.userId=$uid";
        $rs=$this->db->query($uQuery);
        return $rs->result();
    }

       function getUserRegister($uid){
        $rQuery="SELECT eid FROM eregistar WHERE userId=$uid";

        $rs=$this->db->query($rQuery);
        if($rs){
                return $rs->result();
        }else{return false;}
    }

    function getEventsWithPag($offset,$n){
        //need to get events with pagination offset
    }

    function saveWishEvent($param){
        //save the wish event
       
    }
    function saveJoinEvent($param){
        //save the join event
        $uid=$param['uid'];
        $eid=$param['eid'];
        $checkQuery="SELECT * FROM eregistar where userId=$uid AND eid=$eid";
        $checkRs=$this->db->query($checkQuery);
        $result=$checkRs->result();
        echo "I got before if";
        if(!empty($result)){
            echo "I got here and nobody";
            return false;// this mean that the user already save this event
        }
        $insertQuery="INSERT INTO eregistar(userId,eid)VALUES($uid,$eid)";
        return $this->db->simple_query($insertQuery);
    }
    function deleteEvent($eids){
        $strEids=implode(",",$eids);
        $deleteQuery="DELETE FROM eregistar where eid in($strEids)";
        $deleteEvent="DELETE FROM vevents where eid in($strEids)";
        $this->db->trans_start();
        $this->db->query($deleteQuery);
        $this->db->query($deleteEvent);
        $this->db->trans_complete();
        return $this->db->trans_status();
    }

    function getEventInfo($eid, $infoTags=null){
        $queryStr="";
        if($infoTags==null){
            $queryStr="SELECT * FROM vevents WHERE eid=$eid";
        }else{
            if(is_array($infoTags)){
                //meaning the info tags is an array
                $queryStr="SELECT ".explode(",",$infoTags)." FROM vevents WHERE eid=$eid";
            }else{
                if(gettype($infoTags)!="string"){
                    //need to return with some sort of error
                    return false;
                }else{
                    $queryStr="SELECT $infoTags FROM vevents WHERE eid=$eid";
                }
            }
        }
        $rs=$this->db->query($queryStr);
        if($rs!=false){
            return $rs->result();
        }else{
            return $rs;
        }
    }
}

?>
